package com.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.pojo.pms.Pms_Attr;
import com.shop.pojo.pms.Pms_Category_Attr;
import com.shop.service.Pms_Attr_Service;
import com.shop.service.Pms_Category_Attr_Service;
import com.shop.service.Pms_Sku_Sale_Value_Service;
import com.shop.to.AttrTo;
import com.shop.utils.LayuiUtils;
import com.shop.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/pmsAttr")
public class Pms_Attr_Controller {
    @Autowired
    public Pms_Attr_Service pms_attr_service;
    @Autowired
    public Pms_Category_Attr_Service pms_category_attr_service;
    @Autowired
    public Pms_Sku_Sale_Value_Service pms_sku_sale_value_service;

    /*
     * @Description  查询全部属性，或者根据属性名来搜索，自带有分页
     * @param page
     * @param pms_attr
     * @return com.shop.utils.LayuiUtils<com.shop.pojo.pms.Pms_Attr>
     * @author liusupeng
     * @date 2020/10/29 19:27
     */
    @GetMapping("/findAllAttr")
public LayuiUtils<Pms_Category_Attr> findAllAttr(Integer page, String name){
 System.out.println(name);
    return pms_category_attr_service.PMS_ATTR_LAYUI_UTILS(page, name);
}
//查询分类下的全部销售属性
    @GetMapping("/getSkuSaleValueByCatelogId")
public R getSkuSaleValueByCatelogId(Long catalog_id){
       return pms_sku_sale_value_service.getSkuSaleValueByCatelogId(catalog_id);
    }
    //远程search需要的服务，来查询当前属性名
    @GetMapping("/findNameByAttrId/{attrId}")
    public AttrTo findNameByAttrId(@PathVariable("attrId") Long attrId){

        return pms_attr_service.findAttrNameByAttrId(attrId);

    }

    /*
     * @Description  用于添加信息
     * @param pms_attr
     * @return com.shop.utils.R
     * @author liusupeng
     * @date 2020/10/29 19:27
     */
@PostMapping("/addAttr")
public R addAttr(Pms_Category_Attr pms_attr){

                 try {
                     pms_category_attr_service.addPms_Attr_category(pms_attr);
                     return R.success();
                 }catch (Exception e){
                     return R.error(500,"当前属性和分类已经存在，不可重复");
                 }

}


    /*
     * @Description  修改一条属性分类中间表信息，
     * @param pms_attr
     * @return com.shop.utils.R
     * @author liusupeng
     * @date 2020/10/29 19:28
     */
    @PutMapping("/putAttrCategory")
    public R putAttrCategory(Pms_Category_Attr pms_attr){

     try {
         return pms_category_attr_service.putPms_Attr_category(pms_attr);
     }catch (Exception e){
         return R.error(500,"当前属性和分类已经存在，不可重复");
     }

    }
    /*
     * @Description  根据ID来对属性表和属性分类表进行删除
     * @param attr_id
     * @return com.shop.utils.R
     * @author liusupeng
     * @date 2020/10/29 19:28
     */
    @DeleteMapping("/delAttr")
    public R delAttr(Integer attr_id){

        return pms_attr_service.delPms_Attr(attr_id);
    }
}
